package com.aigou.admin.controller;

import com.aigou.admin.entity.*;
import com.aigou.admin.service.Orderservice;
import com.aigou.admin.service.impl.OrderserviceImpl;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
@WebServlet("/order")

public class OrderController extends BaseServlet {

    private Orderservice orderservice = new OrderserviceImpl();

    public void page(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");
        PageEntity<Order> orderPageEntity = orderservice.findOrderPage(Integer.valueOf(current),Integer.valueOf(pageSize));
        ResponseEntity.ok(resp,orderPageEntity);
    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Order> orders = orderservice.findAll();
        ResponseEntity.ok(resp, orders);
    }

    public void find(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Order order = orderservice.findByid(id);
        ResponseEntity.ok(resp, order);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        boolean rs = orderservice.delete(id);
        ResponseEntity.ok(resp, rs);
    }

    public void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String create_time = req.getParameter("create_time");
        String receiving_address = req.getParameter("receiving_address");
        String user_id = req.getParameter("user_id");
        Order order = new Order(id,Timestamp.valueOf(create_time),receiving_address,user_id);
        boolean rs = orderservice.save(order);
        ResponseEntity.ok(resp, rs);
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String create_time = req.getParameter("create_time");
        String receiving_address = req.getParameter("receiving_address");
        String user_id = req.getParameter("user_id");

        Order order = new Order(id,Timestamp.valueOf(create_time),receiving_address,user_id);
        boolean rs = orderservice.update(order);
        ResponseEntity.ok(resp, rs);
    }

}
